package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
      @Autowired
       private DishMapper dishMapper;
      @Autowired
      private DishFlavorsMapper dishFlavorsMapper;
      @Autowired
      private SetmealDishMapper setmealDishMapper;
      @Autowired
      private SetmealMapper setmealMapper;
    /**
     *插入菜品 和相应的口味
     * @param dishDTO
     */
    public void save(DishDTO dishDTO) {
      Dish dish= new Dish();

      BeanUtils.copyProperties(dishDTO,dish);
          //新增一种菜品
        dishMapper.save(dish);
        //获取菜品的主键值
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();

        if(flavors!=null&&flavors.size()>0){
            flavors.forEach(flavor ->
                    flavor.setDishId(id)
                    );
            //新增多个口味
            dishFlavorsMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        Page<DishVO> page=dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //更具id查询到菜品的状态 能否删除
        for (Long id : ids) {
        Dish dish= dishMapper.getId(id);
        if (dish.getStatus()== StatusConstant.ENABLE){
            //当前菜品起售中不能删除
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        }
        //跟据菜品id查询是否关联套餐 能否删除

      List<Long> setmealIds=setmealDishMapper.getSetmealIdVByDishId(ids);
        if (setmealIds!=null&&setmealIds.size()>0){
            //说明有套餐关联
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //直接删除
        for (Long id : ids) {
            dishMapper.delete(id);
            //根据菜品id删除菜品的关联口味
            dishFlavorsMapper.delete(id);
        }
    }

    /**
     * 根据id查询菜品和口味
     * @param id
     * @return
     */
    public DishVO getByIdWithFlavor(Long id) {
        //跟据id查询菜品
       Dish dish=dishMapper.getId(id);

       //根据id查询菜品口味
      List<DishFlavor>  dishFlavors= dishFlavorsMapper.getByDishId(id);

      //将查到的菜品和口味穿回去
      DishVO dishVO=new DishVO();
      BeanUtils.copyProperties(dish,dishVO);
      dishVO.setFlavors(dishFlavors);
      return dishVO;
    }

    /**
     * 修改菜品和口味
     * @param dishDTO
     */
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //修改菜品基本信息
        dishMapper.update(dish);
        //先删除口味信息
        dishFlavorsMapper.delete(dishDTO.getId());
        //在插入口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            flavors.forEach(flavor ->
                    flavor.setDishId(dishDTO.getId())

            );
            dishFlavorsMapper.insertBatch(flavors);
    }
}

    /**
     * 菜品启用禁用
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {

       /* D category=new Category();
        category.setId(id);
        category.setStatus(status);
        categoryMapper.update(category);*/

        Dish  dish=new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.update(dish);

        if (status == StatusConstant.DISABLE) {
            // 如果是停售操作，还需要将包含当前菜品的套餐也停售
            List<Long> dishIds = new ArrayList<>();
            dishIds.add(id);
            // select setmeal_id from setmeal_dish where dish_id in (?,?,?)
            List<Long> setmealIds = setmealDishMapper.getSetmealIdVByDishId(dishIds);
            if (setmealIds != null && setmealIds.size() > 0) {
                for (Long setmealId : setmealIds) {
                    Setmeal setmeal = Setmeal.builder()
                            .id(setmealId)
                            .status(StatusConstant.DISABLE)
                            .build();
                    setmealMapper.update(setmeal);
                }
            }
        }
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    public List<Dish> getDishBycategoryId(Long categoryId) {
      Dish dish=new Dish();
      dish.setCategoryId(categoryId);
      dish.setStatus(1);

        return dishMapper.list(dish);
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorsMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }


}
